<template>
  <div class="analysis-tools-panel">
    <!-- 工具列表 -->
    <div class="tools-grid">
      <div 
        v-for="tool in analysisTools" 
        :key="tool.id"
        class="tool-card"
        :class="{ 'active': selectedTool && selectedTool.id === tool.id }"
        @click="selectTool(tool)"
      >
        <div class="tool-icon">
          <i :class="tool.icon"></i>
        </div>
        <div class="tool-info">
          <div class="tool-name">{{ tool.name }}</div>
          <div class="tool-desc">{{ tool.description }}</div>
        </div>
      </div>
    </div>
    
    <!-- 选中工具的详情 -->
    <div class="tool-details" v-if="selectedTool">
      <div class="details-header">
        <h3>{{ selectedTool.name }}</h3>
        <p>{{ selectedTool.description }}</p>
      </div>
      
      <div class="details-params">
        <h4>参数设置</h4>
        
        <div class="param-form">
          <template v-for="(param, index) in selectedTool.params" :key="index">
            <!-- 文本输入 -->
            <div class="param-item" v-if="param.type === 'text'">
              <div class="param-label">
                <span>{{ param.label }}</span>
                <el-tooltip v-if="param.tip" :content="param.tip" placement="top">
                  <i class="fa fa-question-circle"></i>
                </el-tooltip>
              </div>
              <el-input 
                v-model="paramValues[param.name]" 
                :placeholder="param.placeholder || '请输入'"
                :type="param.textarea ? 'textarea' : 'text'"
                :rows="param.textarea ? 4 : 1"
              />
            </div>
            
            <!-- 选择器 -->
            <div class="param-item" v-else-if="param.type === 'select'">
              <div class="param-label">
                <span>{{ param.label }}</span>
                <el-tooltip v-if="param.tip" :content="param.tip" placement="top">
                  <i class="fa fa-question-circle"></i>
                </el-tooltip>
              </div>
              <el-select 
                v-model="paramValues[param.name]" 
                :placeholder="param.placeholder || '请选择'"
                style="width: 100%;"
              >
                <el-option 
                  v-for="option in param.options" 
                  :key="option.value" 
                  :label="option.label" 
                  :value="option.value"
                />
              </el-select>
            </div>
            
            <!-- 日期选择器 -->
            <div class="param-item" v-else-if="param.type === 'date'">
              <div class="param-label">
                <span>{{ param.label }}</span>
                <el-tooltip v-if="param.tip" :content="param.tip" placement="top">
                  <i class="fa fa-question-circle"></i>
                </el-tooltip>
              </div>
              <el-date-picker 
                v-model="paramValues[param.name]" 
                :type="param.dateType || 'date'"
                :placeholder="param.placeholder || '请选择日期'"
                style="width: 100%;"
              />
            </div>
            
            <!-- 数字输入 -->
            <div class="param-item" v-else-if="param.type === 'number'">
              <div class="param-label">
                <span>{{ param.label }}</span>
                <el-tooltip v-if="param.tip" :content="param.tip" placement="top">
                  <i class="fa fa-question-circle"></i>
                </el-tooltip>
              </div>
              <el-input-number 
                v-model="paramValues[param.name]" 
                :min="param.min" 
                :max="param.max"
                :step="param.step || 1"
                style="width: 100%;"
              />
            </div>
          </template>
        </div>
      </div>
      
      <div class="details-actions">
        <el-button @click="resetParams">重置参数</el-button>
        <el-button type="primary" @click="useTool" :disabled="!canUseTool">
          使用工具
        </el-button>
      </div>
    </div>
    
    <div class="empty-selection" v-else>
      <div class="empty-icon">
        <i class="fa fa-hand-pointer-o"></i>
      </div>
      <p>从左侧选择一个分析工具</p>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, reactive, watch } from 'vue';
import { ElMessage } from 'element-plus';

// 定义事件
const emit = defineEmits(['use-tool']);

// 选中的工具
const selectedTool = ref(null);

// 参数值
const paramValues = reactive({});

// 计算属性：是否可以使用工具
const canUseTool = computed(() => {
  if (!selectedTool.value) return false;
  
  // 检查所有必填参数是否已填写
  for (const param of selectedTool.value.params) {
    if (param.required && (paramValues[param.name] === undefined || paramValues[param.name] === null || paramValues[param.name] === '')) {
      return false;
    }
  }
  
  return true;
});

// 监听选中工具的变化，重置参数值
watch(selectedTool, (newTool) => {
  resetParams();
});

// 选择工具
function selectTool(tool) {
  selectedTool.value = tool;
}

// 重置参数
function resetParams() {
  if (!selectedTool.value) return;
  
  // 初始化所有参数的默认值
  for (const param of selectedTool.value.params) {
    paramValues[param.name] = param.default !== undefined ? param.default : null;
  }
}

// 使用工具
function useTool() {
  if (!canUseTool.value) {
    ElMessage.warning('请填写所有必填参数');
    return;
  }
  
  emit('use-tool', selectedTool.value, paramValues);
}

// 工具列表数据
const analysisTools = [
  {
    id: 'financial_analysis',
    name: '财务分析',
    icon: 'fa fa-bar-chart',
    description: '分析企业财务数据，包括资产负债表、利润表等',
    params: [
      {
        name: 'period',
        label: '分析周期',
        type: 'select',
        required: true,
        options: [
          { value: 'quarterly', label: '季度' },
          { value: 'halfYear', label: '半年' },
          { value: 'annual', label: '年度' }
        ],
        default: 'annual'
      },
      {
        name: 'financialData',
        label: '财务数据',
        type: 'text',
        required: true,
        textarea: true,
        placeholder: '请输入财务数据（可粘贴Excel数据）',
        tip: '输入资产负债表、利润表等财务数据，支持粘贴Excel表格数据'
      }
    ]
  },
  {
    id: 'profitability_analysis',
    name: '盈利能力分析',
    icon: 'fa fa-line-chart',
    description: '分析企业盈利能力指标，如净利率、ROE等',
    params: [
      {
        name: 'revenue',
        label: '营业收入',
        type: 'number',
        required: true,
        min: 0,
        placeholder: '请输入营业收入'
      },
      {
        name: 'netProfit',
        label: '净利润',
        type: 'number',
        required: true,
        placeholder: '请输入净利润'
      },
      {
        name: 'totalAssets',
        label: '总资产',
        type: 'number',
        required: true,
        placeholder: '请输入总资产'
      },
      {
        name: 'equity',
        label: '所有者权益',
        type: 'number',
        required: true,
        placeholder: '请输入所有者权益'
      }
    ]
  },
  {
    id: 'cash_flow_analysis',
    name: '现金流分析',
    icon: 'fa fa-money',
    description: '分析企业现金流状况，评估资金流动性',
    params: [
      {
        name: 'operatingCashFlow',
        label: '经营活动现金流量',
        type: 'number',
        required: true,
        placeholder: '请输入经营活动现金流量'
      },
      {
        name: 'investingCashFlow',
        label: '投资活动现金流量',
        type: 'number',
        required: true,
        placeholder: '请输入投资活动现金流量'
      },
      {
        name: 'financingCashFlow',
        label: '筹资活动现金流量',
        type: 'number',
        required: true,
        placeholder: '请输入筹资活动现金流量'
      },
      {
        name: 'period',
        label: '分析期间',
        type: 'date',
        dateType: 'daterange',
        required: true,
        placeholder: '请选择分析期间'
      }
    ]
  },
  {
    id: 'operational_efficiency',
    name: '运营效率分析',
    icon: 'fa fa-cogs',
    description: '分析企业运营效率，包括周转率、运营周期等',
    params: [
      {
        name: 'inventory',
        label: '平均存货',
        type: 'number',
        required: true,
        placeholder: '请输入平均存货'
      },
      {
        name: 'receivables',
        label: '平均应收账款',
        type: 'number',
        required: true,
        placeholder: '请输入平均应收账款'
      },
      {
        name: 'costOfSales',
        label: '销售成本',
        type: 'number',
        required: true,
        placeholder: '请输入销售成本'
      },
      {
        name: 'salesRevenue',
        label: '销售收入',
        type: 'number',
        required: true,
        placeholder: '请输入销售收入'
      }
    ]
  },
  {
    id: 'debt_analysis',
    name: '债务分析',
    icon: 'fa fa-credit-card',
    description: '分析企业债务状况，评估偿债能力和风险',
    params: [
      {
        name: 'shortTermDebt',
        label: '短期债务',
        type: 'number',
        required: true,
        placeholder: '请输入短期债务'
      },
      {
        name: 'longTermDebt',
        label: '长期债务',
        type: 'number',
        required: true,
        placeholder: '请输入长期债务'
      },
      {
        name: 'totalAssets',
        label: '总资产',
        type: 'number',
        required: true,
        placeholder: '请输入总资产'
      },
      {
        name: 'ebit',
        label: '息税前利润(EBIT)',
        type: 'number',
        required: true,
        placeholder: '请输入息税前利润'
      }
    ]
  },
  {
    id: 'growth_analysis',
    name: '增长分析',
    icon: 'fa fa-area-chart',
    description: '分析企业各项指标的增长率和趋势',
    params: [
      {
        name: 'dataType',
        label: '数据类型',
        type: 'select',
        required: true,
        options: [
          { value: 'revenue', label: '营业收入' },
          { value: 'profit', label: '净利润' },
          { value: 'assets', label: '总资产' }
        ],
        default: 'revenue'
      },
      {
        name: 'historicalData',
        label: '历史数据',
        type: 'text',
        required: true,
        textarea: true,
        placeholder: '请输入历史数据（格式：年份,数值;年份,数值）',
        tip: '按照"年份,数值;年份,数值"的格式输入，例如：2020,100;2021,120;2022,150'
      }
    ]
  },
  {
    id: 'competitive_analysis',
    name: '竞争力分析',
    icon: 'fa fa-trophy',
    description: '分析企业与行业竞争对手的对比情况',
    params: [
      {
        name: 'industry',
        label: '所属行业',
        type: 'select',
        required: true,
        options: [
          { value: 'technology', label: '科技' },
          { value: 'finance', label: '金融' },
          { value: 'retail', label: '零售' },
          { value: 'manufacturing', label: '制造业' },
          { value: 'healthcare', label: '医疗健康' },
          { value: 'other', label: '其他' }
        ]
      },
      {
        name: 'competitors',
        label: '主要竞争对手',
        type: 'text',
        required: true,
        placeholder: '请输入主要竞争对手（用逗号分隔）'
      },
      {
        name: 'companyData',
        label: '本公司数据',
        type: 'text',
        required: true,
        textarea: true,
        placeholder: '请输入本公司关键指标数据'
      }
    ]
  },
  {
    id: 'market_analysis',
    name: '市场分析',
    icon: 'fa fa-globe',
    description: '分析企业所在市场环境、规模和趋势',
    params: [
      {
        name: 'market',
        label: '目标市场',
        type: 'text',
        required: true,
        placeholder: '请输入目标市场'
      },
      {
        name: 'marketSize',
        label: '市场规模',
        type: 'number',
        required: true,
        placeholder: '请输入市场规模（单位：亿元）'
      },
      {
        name: 'marketGrowth',
        label: '市场年增长率',
        type: 'number',
        required: true,
        min: -100,
        max: 1000,
        step: 0.1,
        placeholder: '请输入市场年增长率（%）'
      },
      {
        name: 'marketShare',
        label: '公司市场份额',
        type: 'number',
        required: true,
        min: 0,
        max: 100,
        step: 0.1,
        placeholder: '请输入公司市场份额（%）'
      }
    ]
  }
];
</script>

<style scoped>
.analysis-tools-panel {
  display: flex;
  gap: 24px;
  height: 350px;
}

.tools-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
  width: 60%;
  height: 100%;
  overflow-y: auto;
  padding-right: 16px;
}

.tool-card {
  display: flex;
  align-items: center;
  padding: 16px;
  border-radius: 8px;
  background-color: #f5f7fa;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
}

.tool-card:hover {
  background-color: #eef1f6;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.tool-card.active {
  background-color: #ecf5ff;
  border-color: #b3d8ff;
}

.tool-icon {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background-color: #409eff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  margin-right: 16px;
  flex-shrink: 0;
}

.tool-info {
  flex: 1;
  min-width: 0;
}

.tool-name {
  font-weight: 600;
  font-size: 15px;
  color: #303133;
  margin-bottom: 4px;
}

.tool-desc {
  font-size: 12px;
  color: #606266;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tool-details, .empty-selection {
  width: 40%;
  background-color: #f8f9fa;
  border-radius: 8px;
  padding: 16px;
  height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.details-header {
  margin-bottom: 16px;
}

.details-header h3 {
  margin: 0 0 8px 0;
  font-size: 18px;
  color: #303133;
}

.details-header p {
  margin: 0;
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}

.details-params {
  flex: 1;
  overflow-y: auto;
}

.details-params h4 {
  margin: 0 0 16px 0;
  font-size: 15px;
  color: #303133;
  padding-bottom: 8px;
  border-bottom: 1px solid #ebeef5;
}

.param-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.param-item {
  display: flex;
  flex-direction: column;
}

.param-label {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  font-size: 14px;
  color: #606266;
}

.param-label i {
  margin-left: 4px;
  color: #909399;
  cursor: help;
}

.details-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid #ebeef5;
}

.empty-selection {
  align-items: center;
  justify-content: center;
  color: #909399;
  text-align: center;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  color: #dcdfe6;
}

.empty-selection p {
  margin: 0;
  font-size: 14px;
}
</style> 